package com.huobao.yunying.utils;

import java.util.List;

import com.huobao.yunying.activitys.muyingquan.FaTieQuanZi;
import com.huobao.yunying.entity.Wuliu;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class PopupFabu {
	private Context c;
	private static PopupWindow m_PopupWindow;
	private static ListView m_ListView;

	public PopupFabu(Context c) {
		this.c = c;
	}

	/**
	 *
	 * @param view
	 */
	private static void initPopupWindow(Context context, int width) {
		if (width == 0)
			width = ScreenUtil.getInstant().getDisWidth(context) - 20;
		m_PopupWindow = new PopupWindow(context);
		m_PopupWindow.setFocusable(true);
		m_PopupWindow.setTouchable(true);
		m_PopupWindow.setOutsideTouchable(true);
		m_PopupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.pinglun_bg));
		m_PopupWindow.setWidth(width);
		m_PopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
	}

	private static void createListView(Context context, List<FaTieQuanZi> list, OnItemClickListener listener) {
		m_ListView = new ListView(context);
		android.widget.AbsListView.LayoutParams layoutParams = new android.widget.AbsListView.LayoutParams(
				android.widget.AbsListView.LayoutParams.WRAP_CONTENT,
				android.widget.AbsListView.LayoutParams.WRAP_CONTENT);
		m_ListView.setLayoutParams(layoutParams);
		m_ListView.setScrollingCacheEnabled(false);
		m_ListView.setOnItemClickListener(listener);
		MyAdapter adapter = new MyAdapter(list, context);
		m_ListView.setAdapter(adapter);
	}

	public static void dismiss() {
		if (m_PopupWindow != null && m_PopupWindow.isShowing())
			m_PopupWindow.dismiss();
	}

	public static void showPop(Context context, List<FaTieQuanZi> list, View dropView, OnItemClickListener listener) {
		initPopupWindow(context,dropView.getWidth());
		createListView(context, list, listener);
		m_PopupWindow.setContentView(m_ListView);  
		m_PopupWindow.showAsDropDown(dropView);
	}

	private static class MyAdapter extends BaseAdapter {

		private List<FaTieQuanZi> list;
		private Context context;

		public MyAdapter(List<FaTieQuanZi> list, Context context) {
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null)
				convertView = LayoutInflater.from(context).inflate(R.layout.item_pop_listview, null);
			TextView textView = (TextView) convertView;
			textView.setText(list.get(position).getGname());
			return convertView;
		}

	}
}
